package com.zhuiyun.project.api.record.mapper;

import com.zhuiyun.project.api.lottery.entity.Lottery;
import com.zhuiyun.project.api.lottery.model.LotteryModel;
import com.zhuiyun.project.api.record.entity.CommitRecord;
import com.zhuiyun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import javax.persistence.GeneratedValue;
import java.util.List;

/**
 * @ClassName LotteryMapper
 * @Description TODO
 * @Author 17179
 * @Date 2024/7/16 15:51
 **/
@Mapper
public interface RecordMapper extends BaseMapper<CommitRecord> {


    /**
     * 如果设备号存在则更新 否则新新增
     * @param record
     * @return
     */
    @Insert("insert into commit_record(record,device_id,type,hundred_one,button_num,type_no,result,option_type,option_name,del_num,project_name,time,flag) values(#{record},#{deviceId},#{type},#{hundredOne},#{buttonNum},#{typeNo},#{result},#{optionType},#{optionName},#{delNum},#{projectName},now(),#{flag})" )
    @Options(keyColumn = "id",keyProperty = "id",useGeneratedKeys = true)
    int saveRecord(CommitRecord record);
    @Insert("insert into commit_record(id,record,device_id,type,hundred_one,button_num,type_no,result,option_type,option_name,del_num,project_name,time) values(#{id},#{record},#{deviceId},#{type},#{hundredOne},#{buttonNum},#{typeNo},#{result},#{optionType},#{optionName},#{delNum},#{projectName},now())"
           + " on duplicate key update" +
            " device_id = values(device_id), " +
            " record = values(record)," +
            " type = values(type)," +
            " hundred_one = values(hundred_one)," +
            " button_num = values(button_num)," +
            " type_no = values(type_no)," +
            " result = values(result)," +
            " option_type = values(option_type)," +
            " option_name = values(option_name)," +
            " del_num = values(del_num)," +
            " project_name = values(project_name)," +
            " time=now();")
    int updateRecord(CommitRecord record);
    @Select("select * from commit_record where type=#{type} and device_id =#{deviceId} and (flag = 0 or flag = 2) and del_flag = 0")
    List<CommitRecord> getRecordByDeviceId(String deviceId,String type);

    @Delete("delete from commit_record")
    void clearRecord();

    @Select("select * from commit_record where type =#{type}  and device_id = #{deviceId} and del_flag = 0 ")
    List<CommitRecord> getDataIntersection(String type, String optionType,String deviceId);

    /**
     * 用与pc端统计
     * @param type
     * @return
     */
    @Select("select `s`.`result` AS `result` from `commit_record` `s` where `s`.`type` = #{type} " +
            "union all select `m`.`result` AS `result` from `my_record` `m` " +
            " union all select `sr`.`result` AS `result` from `submit_record` `sr` where sr.type=#{type}  and lottery_no=#{lotteryNew};")
    List<String> getResultData(String type,String lotteryNew);

    /**
     * 用于排序模块去交集  删除的号码不排除也需要统计
     * @param type
     * @return
     */
    @Select("select `s`.`result` AS `result` from `commit_record` `s` where `s`.`type` = #{type} " +
            "union all select `m`.`result` AS `result` from `my_record` `m` " +
            "union all select `sr`.`result` AS `result` from `submit_record` `sr` " +
            " where sr.type=#{type}  and sr.lottery_no=#{lotteryNew} ")
    List<String> getRecordBySort(String type,String lotteryNew);

    /**
     * 用于ai模块去交集
     * @param type
     * @return
     */
    @Select("select `s`.`result` AS `result` from `commit_record` `s` where `s`.`type` = #{type}  " + //and s.del_flag = 0 " +
            " union all select `m`.`result` AS `result` from `my_record` `m` " +
            " union all select `sr`.`result` AS `result` from `submit_record` `sr` " +
            " where sr.type=#{type}  and sr.lottery_no=#{lotteryNew}")
    List<String> getAiRecordByType(String type,String lotteryNew);
    @Select("SELECT MAX(id) as max_id FROM commit_record c where c.type = #{type} and device_id = #{phone} and flag =1 and del_flag = 0")
    int getMaxIdByPhoneAndType(String type, String phone);

    @Delete("update  commit_record set del_flag =1 where  type=#{type} and flag = #{delFlag} and device_id=#{phone}")
    int deleteAllByDelFlagAndPhone(int delFlag, String phone,String type);
    @Delete("update  commit_record set del_flag =1 where type=#{type} and device_id=#{phone} and (flag = 0 or flag =2)")
    int deleteAllByTypeAndPhone(String phone,String type);

    @Delete("update commit_record set del_flag =1 where id=#{maxId}")
    int updateRecordDelFlag(int maxId);
}
